<template>
  <div>
    <div class="search-box">
      <el-input
        style="width:140px;margin-right:10px"
        size="medium"
        v-model="position"
        placeholder="岗位名称"
      ></el-input>
      <el-button type="warning" size="small" plain icon="el-icon-search" @click="search">搜索</el-button>
      <el-button type="success" size="small" plain icon="el-icon-circle-plus" @click="add">新增</el-button>
    </div>
    <div class="pageBox">
      <el-pagination
        background
        layout="total"
        :total="total"
        style="display:inline-block;vertical-align: top;"
      ></el-pagination>
      <!-- <el-button type="success" size="small" icon="el-icon-circle-plus" @click="add">新增</el-button>
      <el-button type="info" size="small" plain icon="el-icon-tickets">导出表格</el-button>-->
    </div>
    <div class="tableBox">
      <el-table
        :cell-style="{padding: '5px 0'}"
        v-loading="loading"
        stripe
        :data="tableData"
        border
        tooltip-effect="dark"
        style="width: 100%"
        height="px"
      >
        <el-table-column type="index" align="center" label="序号" width="100"></el-table-column>
        <el-table-column prop="position" label="岗位名称"></el-table-column>
        <el-table-column prop="defaultValidationDays" label="有效期(天)"></el-table-column>
        <el-table-column prop="defaultProtectionDays" label="保护期(天)"></el-table-column>
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button type="primary" size="small" icon="el-icon-edit" @click="modify(scope.row)">编辑</el-button>
            <el-button
              type="danger"
              size="small"
              icon="el-icon-error"
              @click="del(scope.row.positionID)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-dialog
      class="dialog"
      title="新增岗位"
      width="750px"
      top="8vh"
      :visible.sync="dialog"
      :modal-append-to-body="false"
    >
      <div class="form-wrapper">
        <div class="form-flex">
          <div class="form-item">
            <el-form :model="form" :rules="rules" ref="form" label-width="110px">
              <el-form-item label="岗位名称" prop="position">
                <el-input v-model="form.position" placeholder="请输入岗位名称"></el-input>
              </el-form-item>
              <el-form-item label="有效期" prop="defaultValidationDays">
                <el-input
                  type="number"
                  v-model.number="form.defaultValidationDays"
                  placeholder="请输入有效期"
                ></el-input>
              </el-form-item>
              <el-form-item label="保护期" prop="defaultProtectionDays">
                <el-input
                  type="number"
                  v-model.number="form.defaultProtectionDays"
                  placeholder="请输入保护期"
                ></el-input>
              </el-form-item>
              <el-form-item class="form-btns">
                <el-button type="primary" @click="submitForm">{{form.positionID?'确定修改':'立即创建'}}</el-button>
                <el-button @click="resetForm">重置</el-button>
              </el-form-item>
            </el-form>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script type="text/ecmascript-6">
import { addPosition, positionList, delPosition, updatePosition, queryPosition } from '@/api'

export default {
  name: 'marker-level',
  components: {
  },
  data() {
    return {
      productID: 0,
      position: '',
      dialog: false,
      loading: true,
      total: 0,
      tableData: [],
      loading: true,
      form: {
        position: '',
        defaultValidationDays: null,
        defaultProtectionDays: null
      },
      rules: {
        position: [
          { required: true, message: '请输入岗位名称', trigger: 'blur' }
        ],
        defaultValidationDays: [
          { required: true, message: '请输入有效期', trigger: 'blur' }
        ],
        defaultProtectionDays: [
          { required: true, message: '请输入保护期', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getData()
  },
  methods: {
    resetForm() {
      this.$refs.form.resetFields()
    },
    submitForm() {
      this.$refs.form.validate((valid) => {
        console.log(this.form)
        if (valid) {
          this.post()
        } else {
          this.$message.error('校验未通过，请补充完整')
          return false
        }
      })
    },
    post() {
      if (this.form.positionID) {
        updatePosition(this.form).then(res => {
          this.$message({
            message: '修改成功',
            type: 'success'
          })
          this.getData()
          this.dialog = false
        })
      } else {
        addPosition(this.form).then(res => {
          this.$message({
            message: '添加成功',
            type: 'success'
          })
          this.getData()
          this.dialog = false
        })
      }
    },
    del(productId) {
      this.$confirm('此操作将永久删除该岗位, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delPosition(productId).then((res) => {
          this.getData()
          this.$message({
            message: '删除成功',
            type: 'success'
          })
        })
      })
    },
    modify(item) {
      this.form = item
      this.dialog = true
    },
    add() {
      this.form = {
        position: '',
        defaultValidationDays: null,
        defaultProtectionDays: null
      }
      this.dialog = true
    },
    output() {
      this.$message.error('导出接口正在调试中')
    },
    search(data) {
      this.loading = true
      queryPosition({ position: this.position }).then((res) => {
        this.loading = false
        this.total = res.length
        this.tableData = res
      })
    },
    getData() {
      this.loading = true
      positionList().then((res) => {
        this.loading = false
        this.total = res.length
        this.tableData = res
      })
    }
  }
}
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
.el-table--fit 
  height calc(100vh - 235px)
</style>
